package index;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.LockObtainFailedException;

public class Indexer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		doIndex();
	}
	
	private static void doIndex() {
		Parser parser = new Parser();

		String[] files = {"nt", "ot", "bom", "quran"};


		int asd = 0;
		int asd2 = 1;
		try {
			ArrayList<ArrayList<Document>> docs = new ArrayList<ArrayList<Document>>();
			Analyzer anal = new MyAnalyzer();
			IndexWriter indexer = new IndexWriter("index", anal, true);
			for(String coll : files) {
				String collection = "rel200/"+coll+"/"+coll+".xml";

				System.out.println("Indexing: "+collection);

				ArrayList<Document> documents = parser.parse(collection);
				docs.add(documents);
				System.out.println("> "+docs.size());
			}
			int nCol = 0;
			nCol++;
			for(Document d : docs.get(docs.size()-1)) {
				if(asd  == 10000) {
					System.out.println("doc: " +(asd2*10000));
					asd = 0; asd2++;
				}
				asd++;

				indexer.addDocument(d);
			}

			System.out.println("Collections fatte: " + nCol);

			System.out.println(asd+" docs indexed");
			System.out.println("Optimizing...");
			indexer.optimize();
			indexer.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 


		System.out.println("---END---");



	}

}

